package com.mao.springbootquartzdemo.controller;

import com.mao.springbootquartzdemo.job.MyJob;
import com.mao.springbootquartzdemo.service.QuartzService;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Mr.Mao
 * @create 2023-02-22 20:12
 */
@RestController
public class QuartzController {

   private Scheduler quartzService;

   @Autowired
    public QuartzController(Scheduler quartzService) {
        this.quartzService = quartzService;
    }

//    @PostMapping("/add")
//    public boolean add(@RequestParam String name, @RequestParam String group, @RequestParam String cron) {
//        return quartzService.addScheduler(name, group, cron);
//    }
//
//    @PostMapping("/update")
//    public boolean update(@RequestParam String name, @RequestParam String group, @RequestParam String cron) {
//        return quartzService.updateScheduler(name, group, cron);
//    }
//
//    @PostMapping("/delete")
//    public boolean delete(@RequestParam String name, @RequestParam String group) {
//        return quartzService.deleteScheduler(name, group);
//    }

    @RequestMapping("createJob")
    public String createJob() throws SchedulerException {
        JobDetail jobDetail = JobBuilder.newJob(MyJob.class).withIdentity("job1", "group1").build();
        CronTrigger trigger = TriggerBuilder.newTrigger().withIdentity("trigger1", "tgroup1")
                .withSchedule(CronScheduleBuilder.cronSchedule("* * * * *  ?")).build();
        quartzService.scheduleJob(jobDetail,trigger);
        quartzService.start();
        return "created!";
    }


}
